import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame {
    JTextField display;
    JPanel panel1;
    JPanel panel2;
    String nowButton;
    public Calculator() {
        this.setBounds(10, 10, 900, 800);
        this.setLocationRelativeTo(null);// 让窗口在屏幕中间显示
        setTitle("计算器");

        // 菜单栏
        JMenuBar menuBar = new JMenuBar();
        JMenu editMenu = new JMenu("菜单");
        JMenuItem copyMenuItem = new JMenuItem("复制");
        JMenuItem pasteMenuItem = new JMenuItem("粘贴");
        JMenuItem exit = new JMenuItem("退出");
        // 子菜单添加到一级菜单
        editMenu.add(copyMenuItem);
        editMenu.add(pasteMenuItem);
        editMenu.add(exit);
        //将一级菜单添加到菜单栏
        menuBar.add(editMenu);
        //将菜单栏添加到JFrame窗口
        this.setJMenuBar(menuBar);
        // 设置 "复制" 子菜单被点击的监听器
        copyMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("复制  被点击");
                String i = display.getText();
                display.copy();
                JButton jb1 = new JButton("复制");
                jb1.addActionListener(this);
            }
        });
        // 设置 "粘贴" 子菜单被点击的监听器
        pasteMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("粘贴  被点击");
                display.paste();
            }
        });
        // 设置 "退出" 子菜单被点击的监听器
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("退出  被点击");
                System.exit(0);
            }
        });
        display = new JTextField("");
        display.setEnabled(true);
        add(display, BorderLayout.NORTH);
        // 监听操作按钮
        ActionListener command = new commandAction();
        panel1 = new JPanel();
        panel1.setLayout(new GridLayout(5, 4, 5, 5));
        addButton1("1", command);
        addButton1("2", command);
        addButton1("3", command);
        addButton1("+", command);
        addButton1("4", command);
        addButton1("5", command);
        addButton1("6", command);
        addButton1("-", command);
        addButton1("7", command);
        addButton1("8", command);
        addButton1("9", command);
        addButton1("*", command);
        addButton1("0", command);
        addButton1(".", command);
        addButton1("=", command);
        addButton1("/", command);

        add(panel1, BorderLayout.SOUTH);

        panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1, 2, 5, 5));
        add(panel2, BorderLayout.CENTER);
        pack(); // 根据组件大小自动调节窗口大小
    }
    public void addButton1(String label, ActionListener listener) {
        JButton button = new JButton(label);
        button.addActionListener(listener);
        panel1.add(button);
    }
    private void addButton2(String label, ActionListener listener) {
        JButton button = new JButton(label);
        button.addActionListener(listener);
        panel2.add(button);
    }
    // 创建内部类，作用于单击按钮执行命令的监听器
    class commandAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            nowButton = event.getActionCommand();

            if (nowButton != "=") {
                display.setText(display.getText() + nowButton);
            }

            if (nowButton.equals("=")) {
                display.setText(jisuan1(display.getText()));
            }

        }
    }
    // 用来计算的方法
    public String jisuan1(String string) {
        StringBuffer sb = new StringBuffer(string);
        int commandCount = 0;// 符号数量
        int j = 0;// 计数器
        for (j = 0; j < sb.length(); j++) {
            if (sb.charAt(j) <= '9' && sb.charAt(j) >= '0' || sb.charAt(j) == '.') {
                continue;
            } else {
                commandCount++;
            }
        }
        // 初始化符号数组
        char[] command = new char[commandCount];
        String[] num = new String[commandCount + 1];
        for (j = 0; j < num.length; j++) {
            num[j] = "";
        }
        // 遍历一遍，吧每个数字存进数字数组，每个符号存进符号数组
        int k = 0;
        for (j = 0; j < sb.length(); j++) {
            if (sb.charAt(j) <= '9' && sb.charAt(j) >= '0' || sb.charAt(j) == '.') {
                num[k] += sb.charAt(j);
                continue;
            } else {
                command[k] = sb.charAt(j);
                k++;
            }
        }
        // 计算结果
        int result = 0;
        for (int i = 0; i < commandCount; i++) {
            // 取前两个数，和第一个操作符，运算
            int num1 = (int)Double.parseDouble(num[i]);
            int num2 = (int)Double.parseDouble(num[i + 1]);
            char cc = command[i];
            switch (cc) {
                case '+':
                    result = num1 + num2;
                    break;
                case '-':
                    result = num1 - num2;
                    break;
                case '*':
                    result = num1 * num2;
                    break;
                case '/':
                    result = num1 / num2;
                    break;
                case '%':
                    result = num1 % num2;
                    break;
                default:
                    break;
            }
            num[i + 1] = String.valueOf(result);
        }

        return String.valueOf(result);
    }
    public static void main(String[] args) {
        new Calculator().setVisible(true);
    }
}